package liuliu.throughwaring.ui.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import net.tsz.afinal.annotation.view.CodeNote;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.onload.IOnloadView;
import liuliu.throughwaring.control.onload.OnloadListener;
import liuliu.throughwaring.model.Key;

/**
 * 联网查询
 * Created by Administrator on 2015/11/25.
 */
public class OnLineLoadUrlActivity extends BaseActivity implements IOnloadView {
    @CodeNote(id = R.id.online_load_title)
    TToolbar mToolbar;
    @CodeNote(id = R.id.id_error_layout)
    RelativeLayout errorLayout;
    @CodeNote(id = R.id.id_webview_layout)
    LinearLayout webviewLinear;
    private WebView webView;
    private ProgressDialog progressDialog;
    OnloadListener myListener;
    Dialog dialog;
    String controler;

    @Override
    public void initViews() {
        setContentView(R.layout.online_load_url);
        Intent intent = getIntent();
        controler = intent.getStringExtra("Controler");
        if (controler.equals("Index")) {
            mToolbar.setCenterTitle("联网扩展查询");
        } else {
            mToolbar.setCenterTitle("联网人员查询");
        }
        webView = new WebView(this);
        webviewLinear.addView(webView, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setCacheMode(webSettings.LOAD_NO_CACHE);
        errorLayout.setVisibility(View.GONE);
        webView.setVisibility(View.GONE);
        dialog = Utils.ProgressDialog(OnLineLoadUrlActivity.this, dialog, "加载中，请稍候...", true);
        dialog.show();
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                getUrl();
            }
        }, 1000);
    }

    private void getUrl() {
        myListener = new OnloadListener(this, this);
        myListener.getView(controler);
    }

    @Override
    public void initEvents() {
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
                errorLayout.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
                errorLayout.setVisibility(View.VISIBLE);
                webView.setVisibility(View.GONE);
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
        mToolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                OnLineLoadUrlActivity.this.finish();
            }
        });
    }

    @Override
    public void getViewResult(boolean isTrue, String mes) {
        if (dialog != null) {
            dialog.dismiss();
        }
        if (isTrue) {
            if (mes.endsWith("&"))
                mes = mes.substring(0, mes.length() - 1);
            if (!Utils.isEmptyString(mes))
                mes += "&rand=" + System.currentTimeMillis();
            String ip = Utils.ReadString(Key.KEY_IP);
            String port = Utils.ReadString(Key.KEY_PORT);
            String url = ip + ":" + port;
            String mUrl = String.format("http://%s%s", url, mes);
            webView.loadUrl(mUrl);
        } else {
            Utils.ToastShort(OnLineLoadUrlActivity.this, "加载失败");
        }
    }
}
